<template>
  <div class="app-container">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="上级菜单">
            <Treeselect
              v-model="form.parentId"
              :options="menuOptions"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级菜单"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="菜单类型" prop="menuType">
            <el-radio-group v-model="form.menuType">
              <el-radio :label="1">目录</el-radio>
              <el-radio :label="2">菜单</el-radio>
              <el-radio :label="3">权限</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item v-if="form.menuType !== 3" label="菜单图标">
            <el-popover
              placement="bottom-start"
              width="460"
              trigger="click"
              @show="$refs['iconSelect'].reset()"
            >
              <IconSelect ref="iconSelect" @selected="selected" />
              <el-input slot="reference" v-model="form.icon" placeholder="点击选择图标" readonly>
                <svg-icon
                  v-if="form.icon"
                  slot="prefix"
                  :icon-class="form.icon"
                  class="el-input__icon"
                  style="height: 32px;width: 16px;"
                />
                <i v-else slot="prefix" class="el-icon-search el-input__icon" />
              </el-input>
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜单名称" prop="menuName">
            <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <el-input-number v-model="form.orderNum" style="width: 100%;" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.menuType !== 3" label="是否外链">
            <el-radio-group v-model="form.isFrame">
              <el-radio :label="0">是</el-radio>
              <el-radio :label="1">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.menuType !== 3" label="路由地址" prop="routerPath">
            <el-input v-model="form.routerPath" placeholder="请输入路由地址" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.menuType === 2" :span="12">
          <el-form-item label="组件路径" prop="componentUrl">
            <el-input v-model="form.componentUrl" placeholder="请输入组件路径" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.menuType !== 1" label="权限标识">
            <el-input v-model="form.permission" placeholder="请权限标识" maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.menuType !== 3" label="显示状态">
            <el-radio-group v-model="form.visible">
              <el-radio :label="1">显示</el-radio>
              <el-radio :label="0">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.menuType !== 3" label="菜单状态">
            <el-radio-group v-model="form.status">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.menuType === 2" label="是否缓存">
            <el-radio-group v-model="form.isCache">
              <el-radio :label="0">缓存</el-radio>
              <el-radio :label="1">不缓存</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="dialog-footer">
      <el-button v-if="!loading" type="primary" @click="add">提 交</el-button>
      <el-button v-else type="primary">提交中...</el-button>
      <el-button @click="reset">重 置</el-button>
    </div>
  </div>
</template>

<script>

import IconSelect from '@/components/IconSelect/index'
import api from '@/api/system/sys-menu'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'SysMenuAdd',
  components: { IconSelect, Treeselect },
  data() {
    return {
      loading: false,
      // 表单对象
      form: {},
      // 菜单树结构
      menuOptions: [],
      // 表单校验
      rules: {
        menuName: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '菜单顺序不能为空', trigger: 'blur' }
        ],
        routerPath: [
          { required: true, message: '路由地址不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getSelectTree()
    this.reset()
  },
  methods: {
    // 选择图标
    selected(name) {
      this.form.icon = name
    },
    add() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          api.add(this.form).then(res => {
            this.$message.success(res.msg)
            // 关闭对话框，并刷新列表
            this.$emit('close')
            this.$emit('after')
            this.reset()
            this.loading = false
          })
        }
      })
    },
    // 构造树形菜单
    getSelectTree() {
      api.getTreeList().then(res => {
        // 清空菜单集合
        this.menuOptions = []
        // 设置顶级菜单默认父级
        const menu = { menuId: 0, menuName: '主类目', children: [] }
        menu.children = res.data
        this.menuOptions.push(menu)
      })
    },
    /**
     * 转换下拉菜单数据结构
     * id: key
     * label: 文字
     * children: 节点
     * @param node
     * @returns {{children: *, id: (number|any|(function(): string)), label: (string|any)}}
     */
    normalizer(node) {
      // 如果没有对象就删除掉
      if (!node.children) {
        delete node.children
      }
      return {
        id: node.menuId,
        label: node.menuName,
        children: node.children
      }
    },
    // 表单重置
    reset() {
      this.form = {
        menuId: undefined,
        parentId: 0,
        menuName: undefined,
        icon: undefined,
        menuType: 1,
        orderNum: 0,
        isFrame: 1,
        isCache: 0,
        visible: 1,
        status: 1
      }
      this.resetForm('form')
    }
  }
}
</script>

<style scoped>

</style>
